home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
UPC12BS1.ZIP
/
UUCICO
/
ULIBIP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-02
|
27KB
|
814 lines
/*--------------------------------------------------------------------*/
/* u l i b i p . c */
/* */
/* TCP/IP port communications driver for Windows sockets */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Copyright (c) David M. Watt 1993, All Rights Reserved */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Changes Copyright (c) 1989-1993 by Kendra Electronic */
/* Wonderworks. */
/* */
/* All rights reserved except those explicitly granted by the */
/* UUPC/extended license agreement. */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* RCS Information */
/*--------------------------------------------------------------------*/
/*
* $Id: ulibip.c 1.6 1993/10/02 23:12:35 dmwatt Exp $
*
* $Log: ulibip.c $
* Revision 1.6 1993/10/02 23:12:35 dmwatt
* Winsock error message support
*
* Revision 1.5 1993/09/26 03:32:27 dmwatt
* Use Standard Windows NT error message module
*
* Revision 1.4 1993/09/25 03:07:56 ahd
* Addition error traps by Dave Watt
*
* Revision 1.3 1993/09/23 03:26:51 ahd
* Correct setting of carrier detect
*
* Revision 1.2 1993/09/21 01:42:13 ahd
* Use standard MAXPACK limit for save buffer size
*
* Revision 1.1 1993/09/20 04:48:25 ahd
* Initial revision
*
*/
/*--------------------------------------------------------------------*/
/* System include files */
/*--------------------------------------------------------------------*/
#include <windows.h>
#include "winsock.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*--------------------------------------------------------------------*/
/* UUPC/extended include files */
/*--------------------------------------------------------------------*/
#include "lib.h"
#include "hlib.h"
#include "ulib.h"
#include "comm.h" // Modem status bits
#include "ssleep.h"
#include "catcher.h"
#include "commlib.h" // Trace functions, etc.
#include "pwserr.h" // Windows sockets error messages
#ifdef _Windows
#include "pwinsock.h" // definitions for 16 bit Winsock functions
#endif
/*--------------------------------------------------------------------*/
/* Internal prototypes */
/*--------------------------------------------------------------------*/
void AtWinsockExit(void);
boolean IsFatalSocketError(int err);
void tcloseline(void);
/*--------------------------------------------------------------------*/
/* Global variables */
/*--------------------------------------------------------------------*/
static boolean carrierDetect = FALSE;
currentfile();
static boolean hangupNeeded = TRUE;
extern boolean winsockActive; // Initialized in catcher.c
static SOCKET pollingSock = INVALID_SOCKET; // The current polling socket
static SOCKET connectedSock = INVALID_SOCKET; // The currently connected socket
static boolean connectionDied = FALSE; // The current connection failed
/*--------------------------------------------------------------------*/
/* Local defines */
/*--------------------------------------------------------------------*/
#ifndef MAKEWORD
#define MAKEWORD(a, b) ((WORD)(((BYTE)(a)) | ((WORD)((BYTE)(b))) << 8))
#endif
/*--------------------------------------------------------------------*/
/* I n i t W i n s o c k */
/* */
/* Start the Windows sockets DLL */
/*--------------------------------------------------------------------*/
boolean InitWinsock(void)
{
WSADATA WSAData;
int status;
static boolean firstPass = TRUE;
if ( winsockActive )
return TRUE;
/*--------------------------------------------------------------------*/
/* The atexit() must precede the WSAStartup() so the */
/* FreeLibrary() call gets done */
/*--------------------------------------------------------------------*/
if ( firstPass )
{
firstPass = FALSE;
atexit(AtWinsockExit);
}
#ifdef _Windows
if (!pWinSockInit())
return FALSE;
#endif
// status = WSAStartup(MAKEWORD(1,1), &WSAData);
status = WSAStartup(0x0101, &WSAData);
if (status != 0)
{
printf("WSAStartup Error: %d", status);
return FALSE;
}
winsockActive = TRUE;
return TRUE;
} /* InitWinsock */
/*--------------------------------------------------------------------*/
/* A t W i n s o c k E x i t */
/* */
/* Clean up Windows DLL at shutdown */
/*--------------------------------------------------------------------*/
void AtWinsockExit(void)
{
WSACleanup();
#ifdef _Windows
pWinSockExit();
#endif
winsockActive = FALSE;
} /* AtWinsockExit */
/*--------------------------------------------------------------------*/
/* t o p e n a c t i v e */
/* */
/* Open an active socket connection for I/O */
/*--------------------------------------------------------------------*/
#ifdef __TURBOC__
#pragma argsused
#endif
int tactiveopenline(char *name, BPS bps, const boolean direct)
{
SOCKADDR_IN sin;
LPHOSTENT phe;
LPSERVENT pse;
if (!InitWinsock()) // Initialize library?
return TRUE; // No --> Report error
if (portActive) /* Was the port already active? */
closeline(); /* Yes --> Shutdown it before open */
printmsg(15, "tactiveopenline: %s", name);
norecovery = FALSE; // Flag we need a graceful shutdown after
// Ctrl-BREAK
carrierDetect = FALSE; /* No modem connected yet */
connectionDied = FALSE; /* The connection hasn't failed yet */
/*--------------------------------------------------------------------*/
/* Get remote host name */
/*--------------------------------------------------------------------*/
sin.sin_family = AF_INET;
phe = gethostbyname(name);
if (phe)
#ifdef _Windows
_fmemcpy((char FAR *) &(sin.sin_addr),
(char FAR *) phe->h_addr,
phe->h_length);
#else
memcpy((char FAR *) &(sin.sin_addr),
(char FAR *) phe->h_addr,
phe->h_length);
#endif
else {
sin.sin_addr.s_addr = inet_addr(name);
if ( sin.sin_addr.s_addr == INADDR_NONE )
{
int wsErr = WSAGetLastError();
printmsg(0, "tactiveopenline: "
"Is '%s' listed in the hosts file or a valid IP address?",
name);
printWSerror("gethostbyname", wsErr);
return TRUE;
}
} /* else */
/*--------------------------------------------------------------------*/
/* Get the TCP/IP port number */
/